package com.controller;

import com.model.profiles.LogProfiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

@EnableConfigurationProperties({LogProfiles.class})
@Controller
public class LogController {

    private Logger log= Logger.getLogger(LogController.class.getName());
    @Autowired
    private LogProfiles logProfiles;

    public List<String> getTestList(){
        List<String> testList = new ArrayList<>();
        testList.add("log");
        testList.add("log2");
        return testList;
    }

    @RequestMapping(value = "/")
    public String getLogView(Model model){
        List<String> logList = getLogList();
        model.addAttribute("logList",logList);
        return "LogView";
    }

    @RequestMapping("/logCata/list")
    @ResponseBody
    public List<String> getLogList(){
        String [] logCmd = new String[] {"/bin/sh","-c",
                String.format("cd %s \n ls", logProfiles.getLogPath1())
        };
        return cmdExecutor(logCmd);
    }

    @RequestMapping("/logCata/{logName}/{lineNum}")
    @ResponseBody
    public String getLog(@PathVariable String logName , @PathVariable Integer lineNum){
            String[] cmd = new String[] { "/bin/sh", "-c",
                    String.format("cd %s \n tail -%d %s",logProfiles.getLogPath1(),lineNum,logName)};
            List<String> resultData = cmdExecutor(cmd);
            StringBuilder sb = new StringBuilder();
            for (String str: resultData){
                sb.append(str).append("\n");
            }
            return sb.toString();
    }

    private List<String> cmdExecutor(String [] cmd){
        try {
            Process ps = Runtime.getRuntime().exec(cmd);

            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            List<String> sb = new LinkedList<>();
            String line;
            while ((line = br.readLine()) != null) {
                if(line.equals("\n")){
                    continue;
                }
                sb.add(line);
            }
            return sb;
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        return "hello world";
    }


    @RequestMapping("/logTest/{logName}/{lineNum}")
    @ResponseBody
    public String getTestLog(@PathVariable String logName , @PathVariable Integer lineNum){
        List<String> testList = new ArrayList<>();
        testList.add("test\n");
        testList.add("test2\n");
        testList.add("test3\n");
        StringBuilder sb = new StringBuilder();
        for (String str: testList){
            sb.append(str);
        }
        return sb.toString();
    }
}
